//
//  LKManagerAlianceListViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/5/26.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKManagerAlianceListViewController.h"
#import "LKManagerAlianceListCell.h"

@interface LKManagerAlianceListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)NSMutableDictionary *paramDic;

@end

@implementation LKManagerAlianceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    [self createTableview];
    [self getListDataWithStatus:self.shopOpenStatu + 1 AndWithpage:self.page andWords:@""];
    
    // Do any additional setup after loading the view.
}
-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKManagerAlianceListCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(10);
        make.bottom.equalTo(self.view).offset(0);
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView reloadData];
}
/// 上拉刷新
-(void)headerReloadData{
    self.page = 0;
    if (self.resignBlock) {
        self.resignBlock();
    }
    [self getListDataWithStatus:self.shopOpenStatu + 1 AndWithpage:self.page andWords:@""];
//    self.paramDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"sort_mode":self.priceType,@"page":@(self.page)}];
//    [self getShopListDataWithParam:self.paramDic];
}

/// 下拉加载更多
-(void)footerReloadData{
    if (self.resignBlock) {
        self.resignBlock();
    }
    self.page++;
    [self getListDataWithStatus:self.shopOpenStatu + 1 AndWithpage:self.page andWords:@""];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKManagerAlianceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKManagerAlianceListCell"];
    cell.model = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 118.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LkMyShopOpenStatusListModel *model = self.dataArray[section];
    UIView *header = [[UIView alloc] init];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectMake(20, 0, WIDTH, 32) text:[NSString timestampSwitchTime:[model.add_time integerValue] andFormatter:@"YYYY-MM-dd"] textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentLeft];
    [header addSubview:headerLabel];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32.f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LkMyShopOpenStatusListModel *model = self.dataArray[indexPath.section];
    if (self.pushBlock) {
        self.pushBlock(model);
    }
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
/// 根据状态获取商户信息
/// @param status 商户进件状态
-(void)getListDataWithStatus:(NSInteger)status AndWithpage:(NSInteger)page andWords:(NSString *)word{
//    [SVProgressHUD show];
    NSString *statusStr = [NSString stringWithFormat:@"%ld",status];
    NSDictionary *param = @{@"token":[LKUserInforTool account].token,@"status":statusStr,@"page":@(page),@"keywords":word,@"userId":[LKUserInforTool account].uid};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopOpenStatusListUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            if (self.page == 0) {
                [self.dataArray removeAllObjects];
            }
            NSArray *dataArra = responseObject[@"data"];
            if (dataArra.count < 15) {
            for (NSDictionary *dic in dataArra) {
                LkMyShopOpenStatusListModel *model = [LkMyShopOpenStatusListModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
                self.tableView.mj_footer.hidden = YES;
            }else{
                for (NSDictionary *dic in dataArra) {
                    LkMyShopOpenStatusListModel *model = [LkMyShopOpenStatusListModel modelWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
            }
                [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
